package cc.autoapi.pucong.auto.flow.provider.api.controller;

import cc.autoapi.pucong.auto.flow.core.root.entity.BatchDeleteIdDto;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowBaseController;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowExceptionHandler;
import cc.autoapi.pucong.auto.flow.core.root.utils.Res;
import cc.autoapi.pucong.auto.flow.provider.api.manager.ApiInfoManager;
import cc.autoapi.pucong.auto.flow.provider.api.entity.dto.ApiInfoAddDto;
import cc.autoapi.pucong.auto.flow.provider.api.entity.dto.ApiInfoTestExecDto;
import cc.autoapi.pucong.auto.flow.provider.api.entity.dto.ApiInfoUpdateDesignDto;
import cc.autoapi.pucong.auto.flow.provider.api.entity.dto.ApiInfoUpdateDto;
import cc.autoapi.pucong.auto.flow.provider.api.entity.qo.ApiInfoQo;
import cc.autoapi.pucong.auto.flow.provider.api.entity.vo.ApiInfoVo;
import cc.autoapi.pucong.auto.flow.provider.api.entity.vo.ApiTestExecResult;
import cc.autoapi.pucong.auto.flow.provider.api.entity.vo.ApiTreeVo;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author pucong
 * @description 接口信息 controller
 * @create 2024-09-24
 */
@RestController
@Validated
@RequestMapping("/api/apiInfo")
public class ApiInfoController extends AutoFlowBaseController implements AutoFlowExceptionHandler{

    @Resource
    private ApiInfoManager apiInfoManager;

    @GetMapping("/getList")
    public Res<List<ApiTreeVo>> getList(ApiInfoQo dto) {
        List<ApiTreeVo> apiInfoList = apiInfoManager.getList(dto);
        return Res.ok(apiInfoList);
    }

    @GetMapping("/getDetailById/{id}")
    public Res<ApiInfoVo> getDetailById(@PathVariable("id") String id) {
        ApiInfoVo apiInfoDto = apiInfoManager.getDetailById(id);
        return Res.ok(apiInfoDto);
    }

    @PostMapping("/add")
    public Res<String> add(@RequestBody ApiInfoAddDto dto) {
        String id = apiInfoManager.add(dto);
        return Res.ok(id);
    }

    @PostMapping("/updateById/{id}")
    public Res<Boolean> updateById(@PathVariable("id") String id,
                                   @RequestBody ApiInfoUpdateDto dto) {
        Boolean i = apiInfoManager.updateById(id, dto);
        return Res.ok(i);
    }

    @PostMapping("/updateDesignById/{id}")
    public Res<Boolean> updateDesignById(@PathVariable("id") String id,
                                         @RequestBody ApiInfoUpdateDesignDto dto) {
        Boolean i = apiInfoManager.updateDesignById(id, dto);
        return Res.ok(i);
    }

    @PostMapping("/enabledById/{id}")
    public Res<Boolean> enabledById(@PathVariable("id") String id) {
        Boolean i = apiInfoManager.enabledById(id);
        return Res.ok(i);
    }

    @PostMapping("/disabledById/{id}")
    public Res<Boolean> disabledById(@PathVariable("id") String id) {
        Boolean i = apiInfoManager.disabledById(id);
        return Res.ok(i);
    }

    @PostMapping("/testExecById/{id}")
    public Res<ApiTestExecResult> testExecById(@PathVariable("id") String id,
                                               @RequestBody ApiInfoTestExecDto dto) {
        ApiTestExecResult apiExecResult = apiInfoManager.testExecById(id, dto);
        return Res.ok(apiExecResult);
    }

    @DeleteMapping("/batchDeleteByIds")
    public Res<Boolean> batchDeleteByIds(BatchDeleteIdDto dto) {
        Boolean i = apiInfoManager.batchDeleteByIds(dto.getIdList());
        return Res.ok(i);
    }

    @PostMapping("/copyApiByApiId/{apiId}")
    public Res<String> copyProjectApiByApiId(@PathVariable("apiId") String apiId) {
        String id = apiInfoManager.copyApiByApiId(apiId);
        return Res.ok(id);
    }

    @GetMapping("/exportApiByApiId/{apiId}")
    public void exportApiByApiId(@PathVariable("apiId") String apiId,
                                 HttpServletResponse response) {
        apiInfoManager.exportApiByApiId(apiId, response);
    }

    @PostMapping("/uploadApiInfo")
    public void uploadApiInfo(@RequestParam("file") MultipartFile file) {
        apiInfoManager.uploadApiInfo(file);
    }
}
